home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 5.4 KB | 139 lines |
- import java.awt.Container;
- import java.awt.Image;
- import java.awt.Graphics;
- import java.awt.Dimension;
-
- /**
- * A component which does all its drawing in an offscreen image
- * the size of the component.
- *
- * @author Levi Brown
- * @author Apple Worldwide Developer Technical Support
- * @author Apple Computer Inc.
- * @version 1.0 11/4/1998
- * Copyright: © Copyright 1999 Apple Computer, Inc. All rights reserved.
- *
- * Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- * ("Apple") in consideration of your agreement to the following terms, and your
- * use, installation, modification or redistribution of this Apple software
- * constitutes acceptance of these terms. If you do not agree with these terms,
- * please do not use, install, modify or redistribute this Apple software.
- *
- * In consideration of your agreement to abide by the following terms, and subject
- * to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- * copyrights in this original Apple software (the "Apple Software"), to use,
- * reproduce, modify and redistribute the Apple Software, with or without
- * modifications, in source and/or binary forms; provided that if you redistribute
- * the Apple Software in its entirety and without modifications, you must retain
- * this notice and the following text and disclaimers in all such redistributions of
- * the Apple Software. Neither the name, trademarks, service marks or logos of
- * Apple Computer, Inc. may be used to endorse or promote products derived from the
- * Apple Software without specific prior written permission from Apple. Except as
- * expressly stated in this notice, no other rights or licenses, express or implied,
- * are granted by Apple herein, including but not limited to any patent rights that
- * may be infringed by your derivative works or by other works in which the Apple
- * Software may be incorporated.
- *
- * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- * COMBINATION WITH YOUR PRODUCTS.
- *
- * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- public class BufferedDrawer extends Container
- {
- /**
- * Creates a new BufferedDrawer.
- */
- public BufferedDrawer()
- {
- //Initialize our data members
- workingGraphics = null;
- workingImage = null;
- }
-
- /**
- * Handles drawing the desired content into the offscreen buffer.
- * Override this to do your own drawing (be sure to call super.draw()).
- * @see #paint
- */
- protected void draw()
- {
- //Make sure the offscreen image is ready to be drawn into.
- ensureValidWorkingImage();
-
- //Do drawing here (use workingGraphics)
- //Don't forget to erase the buffer if you don't draw over the entire area
- //or else you will end up with tracers.
- //To erase the entire area, uncomment out the following code:
- }
-
- /**
- * Makes sure the offscreen image exists.
- */
- protected void ensureValidWorkingImage()
- {
- //If the size changed and we need to create a new pane image, then create one
- Dimension size = getSize();
- if ((size != null && size.width > 0 && size.height > 0) && (workingImage == null || size.width != workingImage.getWidth(this) || size.height != workingImage.getHeight(this)))
- {
- if (workingImage != null)
- {
- workingImage.flush();
- workingImage = null;
- }
- workingImage = createImage(size.width, size.height);
- if (workingGraphics != null)
- {
- workingGraphics.dispose();
- workingGraphics = null;
- }
- workingGraphics = workingImage == null ? null : workingImage.getGraphics();
- }
- }
-
- /**
- * Updates the component. This method is called in
- * response to a call to repaint. You can assume that
- * the background is not cleared.
- * Overriden here to prevent unwanted background erasing.
- * @param g the specified Graphics window
- * @see #paint
- * @see java.awt.Component#repaint
- */
- public void update(Graphics g)
- {
- paint(g);
- }
-
- /**
- * Paints the component. This method is called when the contents
- * of the component should be painted in response to the component
- * first being shown or damage needing repair. The clip rectangle
- * in the Graphics parameter will be set to the area which needs
- * to be painted.
- * @param g the specified Graphics object
- * @see #update
- */
- public void paint(Graphics g)
- {
- draw();
- g.drawImage(workingImage, 0, 0, this);
- super.paint(g);
- }
-
- //The offscreen image buffer to render the progress bar into.
- protected Image workingImage;
- //The Graphics object associated with the offscreen image buffer.
- protected Graphics workingGraphics;
- }
-